Java 使用IO流读取txt文件 以及避坑点

您所在的位置:网站首页 import javaio Java 使用IO流读取txt文件 以及避坑点

Java 使用IO流读取txt文件 以及避坑点

#Java 使用IO流读取txt文件 以及避坑点| 来源: 网络整理| 查看: 265

一. 编程题 2.1 注解,文件IO和字符串解析操作 已知文件 jdbc.txt className=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/gp_01 username=root password=root 要求针对于注解,注解用于在代码中提供 jdbc.txt 文件路径利用 io 读取文件内容将 文件内容读取存储到 HashMap 中

private static HashMap map1 = new HashMap(); public static void main(String[] args) throws Exception { Demo1.tFileInputStream(); } public static void tFileInputStream() throws Exception{ FileInputStream fileInputStream = new FileInputStream("C:/Users/12997/Documents/WeChat Files/wxid_ckew8ta6dsu622/FileStorage/File/2023-03/周考/jdbc.txt"); byte[] bytes = new byte[1024*5]; int len = 0; StringBuilder sb = new StringBuilder(); String str = bytes.toString(); while ((len = fileInputStream.read(bytes))!=-1){ sb.append(new String(bytes,0,len)); } fileInputStream.close(); String s = sb.toString(); String[] split1 = s.split("\r\n"); // 必须先以换行分割,要不然先以等号的话,会导致,上一行的末尾,和下一行的开头分到同一个数组结点内 // 例如 上一行的末尾 com.mysql.jdbc.Driver 和下一行的开头 username 分到一个数组结点内 // for (String s1 : split1) { //System.out.println("s1 = " + s1); //String[] split = s1.split("="); int index = s1.indexOf("="); map1.put(s1.substring(0,index),s1.substring(index + 1)); } //增强for循环 map1.forEach((key, value) -> { System.out.println(key); System.out.println(value); }); //迭代器有点low // SetentrySet = map1.entrySet(); // Iterator iterator = entrySet.iterator(); // while (iterator.hasNext()) { // Map.Entry next = iterator.next(); // System.out.println(next.getKey()); // //System.out.println(next.getValue()); // System.out.println(); // } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3